package co.com.umobile.persistence;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import co.com.umobile.application.GlobalState;

public class UmobileProvider extends ContentProvider {

	/** the search provider authority. */
	public static final String AUTHORITY = "co.com.umobile.persistence.UmobileProvider";

	/** the search content URI. */
	public static final Uri CONTENT_URI_SUBJECT_LIST = Uri.parse("content://" + AUTHORITY + "/subjectList");

	/** the search content URI. */
	public static final Uri CONTENT_URI_PLACE_LIST = Uri.parse("content://" + AUTHORITY + "/placeList");
	
	/** the search content URI. */
	public static final Uri CONTENT_URI_NOTIFICATION_LIST = Uri.parse("content://" + AUTHORITY + "/notificationList");

	/** the search content URI. */
	public static final Uri CONTENT_URI_SUBJECT__DETAIL_LIST = Uri.parse("content://" + AUTHORITY + "/subjectDetailList");

	/** the search content URI. */
	public static final Uri CONTENT_URI_SCHEDULE_DETAIL_LIST = Uri.parse("content://" + AUTHORITY + "/schedule_detail_list");

	/** the uri matcher. */
	private static final UriMatcher URI_MATCHER = buildUriMatcher();

	/** Subject list items. */
	private static final int SUBJECT_LIST_ITEMS = 1;

	/** place list items. */
	private static final int PLACE_LIST_ITEMS = 2;

	/** Subject detail list items. */
	private static final int SUBJECT_DETAIL_LIST_ITEMS = 3;

	/** schedule detail list items. */
	private static final int SCHEDULE_DETAIL_LIST_ITEMS = 4;
	
	/** NOTIFICATION detail list items. */
	private static final int NOTIFICATION_LIST_ITEMS = 5;

	/** globalState is used to transfer data to other activity. */
	private GlobalState globalState;

	@Override
	public boolean onCreate() {
		globalState = (GlobalState) getContext();
		DatabaseManager.getInstance(getContext());
		return true;
	}

	/**
	 * Builds up a UriMatcher for search suggestion and shortcut refresh
	 * queries.
	 * 
	 * @return Uri matcher.
	 */
	private static UriMatcher buildUriMatcher() {
		UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
		// to get definitions...
		matcher.addURI(AUTHORITY, "subjectList", SUBJECT_LIST_ITEMS);
		matcher.addURI(AUTHORITY, "subjectList/#", SUBJECT_LIST_ITEMS);

		matcher.addURI(AUTHORITY, "placeList", PLACE_LIST_ITEMS);
		matcher.addURI(AUTHORITY, "placeList/#", PLACE_LIST_ITEMS);
		
		matcher.addURI(AUTHORITY, "notificationList", NOTIFICATION_LIST_ITEMS);
		matcher.addURI(AUTHORITY, "notificationList/#", NOTIFICATION_LIST_ITEMS);
		
		matcher.addURI(AUTHORITY, "subjectDetailList", SUBJECT_DETAIL_LIST_ITEMS);
		matcher.addURI(AUTHORITY, "subjectDetailList/#", SUBJECT_DETAIL_LIST_ITEMS);

		matcher.addURI(AUTHORITY, "schedule_detail_list", SCHEDULE_DETAIL_LIST_ITEMS);
		matcher.addURI(AUTHORITY, "schedule_detail_list/#", SCHEDULE_DETAIL_LIST_ITEMS);

		return matcher;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

		switch (URI_MATCHER.match(uri)) {
		case SUBJECT_LIST_ITEMS:

			Cursor cursor = DatabaseManager.getDatabase().rawQuery(
					"SELECT rowId as _id, subjectCode, subjectName, definitiveGrade FROM Subject", null);

			if (cursor == null) {
				return null;
			} else if (!cursor.moveToFirst()) {
				cursor.close();
				return null;
			}

			return cursor;
		case SCHEDULE_DETAIL_LIST_ITEMS:

			Cursor cursorSchedule = DatabaseManager.getDatabase().rawQuery(
					"SELECT s.rowId as _id, subjectDate, subjectTime, block, room, s.placeCode, s.subjectDetailCode  FROM SubjectDetail s LEFT OUTER JOIN Place p ON (s.placeCode = p.placeCode) WHERE s.subjectCode = ?", new String[] { selection });

			if (cursorSchedule == null) {
				return null;
			} else if (!cursorSchedule.moveToFirst()) {
				cursorSchedule.close();
				return null;
			}

			return cursorSchedule;
		case SUBJECT_DETAIL_LIST_ITEMS:

			Cursor cursorDetail = DatabaseManager
					.getDatabase()
					.rawQuery(
							"SELECT rowId as _id, subjectPeriod, grade1, grade2, grade3, grade4, finalGrade, definitiveGrade  FROM Subject WHERE subjectCode = ?",
							new String[] { selection });

			if (cursorDetail == null) {
				return null;
			} else if (!cursorDetail.moveToFirst()) {
				cursorDetail.close();
				return null;
			}

			return cursorDetail;
		case PLACE_LIST_ITEMS:

			Cursor cursorPlace = DatabaseManager.getDatabase().rawQuery(
					"SELECT rowId as _id, placeName, placeDescription, block, room,placeCode FROM Place", null);

			if (cursorPlace == null) {
				return null;
			} else if (!cursorPlace.moveToFirst()) {
				cursorPlace.close();
				return null;
			}

			return cursorPlace;
			
		case NOTIFICATION_LIST_ITEMS:

			Cursor cursorNotification = DatabaseManager.getDatabase().rawQuery(
					"SELECT n.rowId as _id, n.date, s.subjectName, n.notificationTypeDescription as type, n.title, n.shortDescription, n.largeDescription FROM Notification n, Subject s where s.subjectCode = n.subjectCode", null);
			
			if (cursorNotification == null) {
				return null;
			} else if (!cursorNotification.moveToFirst()) {
				cursorNotification.close();
				return null;
			}

			return cursorNotification;
			
		default:
			throw new IllegalArgumentException("Unknown Uri: " + uri);
		}
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		return 0;
	}

}
